package control
{
	
	import flash.net.URLVariables;
	
	import model.ModelLocator;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	
	/**
     * made by ws@2008-12-15 15:30 :)
     * the part realize the Friends function.
     */
    
     
	public class showUsers
	{
		internal var theModel:ModelLocator = ModelLocator.getInstance();
		private var showUsersHttp:HTTPService;
		private var showUsersDetailsHttp:HTTPService;
		private var showFriendsHttp:HTTPService;
		private var addFriendHttp:HTTPService;
		private var deleteFriendHttp:HTTPService;
		//private var dbShow:showUsers = new showUsers();
		
		
		public function showUsers()
		{
			showUsersHttp = new HTTPService();
			showUsersHttp.url = "http://localhost:8080/GMdb/servlet/allUsers";
			showUsersHttp.addEventListener(ResultEvent.RESULT,usersResultHandler);
			showUsersHttp.addEventListener(FaultEvent.FAULT, fHandler);
		
			showUsersDetailsHttp = new HTTPService();
			showUsersDetailsHttp.url = "http://localhost:8080/GMdb/servlet/userDetails";
			showUsersDetailsHttp.addEventListener(ResultEvent.RESULT,detailsResultHandler);
			showUsersDetailsHttp.addEventListener(FaultEvent.FAULT, fHandler);
			//Alert.show("!!!!kingdouble here");
			
			showFriendsHttp = new HTTPService();
			showFriendsHttp.url = "http://localhost:8080/GMdb/servlet/allFriends";
			showFriendsHttp.addEventListener(ResultEvent.RESULT,friendsResultHandler);
			showFriendsHttp.addEventListener(FaultEvent.FAULT, fHandler);
			
			addFriendHttp = new HTTPService();
			addFriendHttp.url = "http://localhost:8080/GMdb/servlet/addFriends";
			addFriendHttp.addEventListener(ResultEvent.RESULT,addfriendResultHandler);
			addFriendHttp.addEventListener(FaultEvent.FAULT, fHandler);
			
			deleteFriendHttp = new HTTPService();
			deleteFriendHttp.url = "http://localhost:8080/GMdb/servlet/deleteFriends";
			deleteFriendHttp.addEventListener(ResultEvent.RESULT,deletefriendResultHandler);
			deleteFriendHttp.addEventListener(FaultEvent.FAULT, fHandler);
		}
		
		public function showFriends(username:String):void{
			//Alert.show(username);//good,print 'nn'
			var params:URLVariables = new URLVariables();
       		params.username = httpEncoding(username);
            //Alert.show(params.toString());//good,print 'username=nn'
            showFriendsHttp.send(params); 
            
		}
		
		private function httpEncoding(param:String):String{
                return encodeURIComponent(param);
        }
		
		public function showTheUsers():void{
			showUsersHttp.send();
			
		}
		
		public function showUsersDetails(userid:int):void{
			var params:URLVariables = new URLVariables();
			params.userid=userid;
			showUsersDetailsHttp.send(params);
			//Alert.show(params.toString());//此处有结果！！！userid=xxx
		}
		
		public function addYourFriend(username:String , userid:int):void{
			var params:URLVariables = new URLVariables();
			params.username=username;
			params.userid=userid;
			//Alert.show(params.username + " and " + params.userid);//print good:"nn ang 6"(ws is 6).
			addFriendHttp.send(params);
			
		}
		
		public function deleteYourFriend(username:String ,userid:int):void{
			var params:URLVariables=new URLVariables();
			params.username=username;
			params.userid=userid;
			//Alert.show(params.username + " and " + params.userid);//print good:"nn ang 6"(ws is 6).
			deleteFriendHttp.send(params);
		}
		
		internal function deletefriendResultHandler(event:ResultEvent):void{
			Alert.show("Remove this user successfully!!!");
			showFriends(theModel.user.user_name);
		}
		
		internal function addfriendResultHandler(event:ResultEvent):void{
			if(event.result.record==0)
			{
				Alert.show("Add friend successfully!!!");
				showFriends(theModel.user.user_name);
			}
			   
			else
			   Alert.show("This user is already in your list!!!");
			
		}
		
		internal function usersResultHandler(event:ResultEvent):void{
       			theModel.allUsers=event.result.Result.user;
       	}
       	
       	
       	internal function detailsResultHandler(event:ResultEvent):void{
       			theModel.allUsersDetails=event.result.Result.userdetail;
       	}
       	
       	internal function friendsResultHandler(event:ResultEvent):void{
       			theModel.allFriends=event.result.Result.friend;
       	}
       	
       	
       	internal function fHandler (event:FaultEvent):void {
         		Alert.show(event.fault.message,"Error1")
        }


	}
}